package com.lzx.iteam.net;

import android.os.Message;
import android.util.Log;
import com.lzx.iteam.bean.GroupManageCreate;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class GetGroupInfoMsg extends CcMsgStructure {

    private static String TAG = "GetGroupInfoMsg";
    Message mCallBack;

    public GetGroupInfoMsg(Message callBack) {
        // Auto-generated constructor stub
        mCallBack = callBack;
    }

    @Override
    public void onReceive(JSONObject data) {
        Log.d(TAG, "OnRecive --->" + data.toString());
        if (mCallBack != null) {
            ArrayList<GroupManageCreate> list = new ArrayList<GroupManageCreate>();
            try {
                JSONObject jsonObject = data.getJSONObject(AsynHttpClient.KEY_CC_DATA);
                JSONArray array = jsonObject.getJSONArray("members");
                for (int i = 0; i < array.length(); i++) {
                    GroupManageCreate bean = new GroupManageCreate();
                    bean.setGroupImg(jsonObject.getString("group_img"));
                    bean.setGroupType(jsonObject.getString("group_type"));
                    bean.setGroupName(jsonObject.getString("group_name"));
                    bean.setContactName(array.getJSONObject(i).getString("contact_name"));
                    bean.setContactId(array.getJSONObject(i).getString("contact_id"));
                    bean.setContactImage(array.getJSONObject(i).getString("contact_image"));
                    bean.setUserId(array.getJSONObject(i).getString("user_id"));
                    bean.setUserType(array.getJSONObject(i).getString("user_type"));
                    list.add(bean);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            mCallBack.obj = list;
            mCallBack.sendToTarget();
        }
    }

    @Override
    public void onError(int error, Object ret) {
        Log.d(TAG, "OnErrorCode ---> " + error);
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }
        }
        if (mCallBack != null) {
            mCallBack.arg1 = error;
            if (!StringUtil.isEmpty(errorMsg)) {
                mCallBack.obj = errorMsg;
            } else {
                mCallBack.obj = Constants.NETWORK_WRROR;
            }
            mCallBack.sendToTarget();
        }
    }

}
